package com.itany.corejava.code07_面向对象.继承;

/**
 * @author: 石小俊
 * @date: 2023年03月08日 11:08
 * @version: 1.0
 */
public class Pet {
    String name;
    private String gender;

    public Pet() {
    }

    public Pet(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}
class Cat extends Pet{

    String name;
    private int age;

    public void test(){
        // 此时默认访问的是子类的name属性
        // 遇到这种情况,尽量通过this关键字与super关键字区分是当前类还是父类
        System.out.println("name:"+name);
        System.out.println("name:"+this.name);
        // 如果想要访问父类的,通过super.name
        System.out.println("父类name:"+super.name);
    }

    public Cat() {
    }

    // 通过super关键字可以调用父类中的属性与方法
    // 通过super(参数列表)调用父类的构造方法
    public Cat(String name, String gender) {
        super(name,gender);
    }
    // 通过super(参数列表)调用父类构造方法时
    // 该代码必须处于子类构造方法代码块的第一行
    public Cat(String name, String gender,int age) {
        super(name,gender);
        this.age = age;
    }

    public Cat(String name, String gender,int age,String name2) {
        super(name,gender);
        this.age = age;
        this.name = name2;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class TestPet{
    public static void main(String[] args) {
        Cat cat = new Cat("汤姆","男",10,"tom");
        cat.test();
    }
}
